import com.itextpdf.html2pdf.ConverterProperties;
import com.itextpdf.html2pdf.HtmlConverter;
import com.itextpdf.html2pdf.attach.impl.DefaultTagWorkerFactory;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfDocumentInfo;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.font.FontInfo;
import com.itextpdf.layout.font.FontProvider;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * htmlconvertToPdf
 */
public class html2Pdf {

    public static void main(String [] args) throws IOException {

        final String path ="/Users/wuxinbo/pdfdemo/html2pdf.pdf";
        final String htmlPath="/Users/wuxinbo/pdfdemo/test_origin.html";
        FileOutputStream fileOutputStream =new FileOutputStream(path);
        PdfWriter writer =new PdfWriter(fileOutputStream);
        PdfDocument pdfDoc =new PdfDocument(writer);
        ConverterProperties props =new ConverterProperties();
        FontProvider fp = new FontProvider();
        fp.addSystemFonts();
        for (FontInfo fontInfo : fp.getFontSet().getFonts()) {
            System.out.println(fontInfo.getFontName());
        }
        fp.addStandardPdfFonts();
//        fp.addDirectory(resources);//The noto-nashk font file (.ttf extension) is placed in the resources

        props.setFontProvider(fp);
        PdfDocumentInfo info =pdfDoc.getDocumentInfo();
        info.setAuthor("wuxinbo");
        info.setKeywords("发票");
        info.setTitle("html2pdf");
        info.setSubject("test");
//        props.setBaseUri(resources);
        //Setup custom tagworker factory for better tagging of headers
//        DefaultTagWorkerFactory tagWorkerFactory = new AccessibilityTagWorkerFactory();
//        props.setTagWorkerFactory(tagWorkerFactory);
        HtmlConverter.convertToPdf(new FileInputStream(htmlPath),pdfDoc,props);
        pdfDoc.close();
    }
}
